﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PreguntaAlExperto.RulesLayer.Entities;
using PreguntaAlExperto.RulesLayer.Services;
using System.Net.Mail;
using System.Net;

namespace PreguntaAlExperto
{
    public partial class showArticle : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] != null)
            {
                this.pnlCommentAction.Visible = true;
                this.pnlMustLoggedToComment.Visible = false;
                Page.ClientScript.RegisterStartupScript(this.GetType(), "KeepMe", "GetInformation();", true);
                this.followicon.Visible = true;
            }
            else
            {
                this.pnlCommentAction.Visible = false;
                this.pnlMustLoggedToComment.Visible = true;
                this.followicon.Visible = false;
                if (!string.IsNullOrEmpty(Request.QueryString["f"]))
                {
                    if (Request.QueryString["f"].ToString() == "1")
                    {
                        this.LogIn1.ShowErrorMessage = true;
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowValidation", "ShowLoginContainer();", true);
                    }
                }
            }
        }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            string _articleName = Request.UrlReferrer.ToString().Split('?')[0].ToString();
            _articleName = _articleName.Split('/')[4];
            ArticlesService _service = new ArticlesService();
            Article _article = _service.getArticleByPath(_articleName);
            _service.CommentArticle(_article.Article_ID, int.Parse(Session[0].ToString()), Server.HtmlEncode(this.txtComment.Text));

            MailMessage _message = new MailMessage();
            _message.From = new MailAddress("no-reply@preguntaalexperto.net", "Pregunta Al Experto");
            _message.IsBodyHtml = true;
            _message.Subject = "Se ha realizado un comentario";
            _message.To.Add(new MailAddress("no-reply@preguntaalexperto.net"));
            foreach (Followers _follower in _service.getFollowers(_article.Article_ID))
            {
                if (_follower.ID != (int)Session["UserID"])
                    _message.Bcc.Add(new MailAddress(_follower.Email));
            }
            _message.Body = "Se ha realizado un comentario en un artículo del cual querías mantenerte informado.\n\n" +
                "Ingresa a <a href=\"http://www.preguntaalexperto.net/articles/" + _articleName + "\">http://www.preguntaalexperto.net/articles/" + _articleName + "</a> para ver el comentario.";
            SmtpClient _sender = new SmtpClient("mail.preguntaalexperto.net");
            _sender.UseDefaultCredentials = false;
            _sender.Credentials = new NetworkCredential("info@preguntaalexperto.net", "ppgmz555");
            _sender.Send(_message);

            Response.Redirect(Request.UrlReferrer.ToString().Split('?')[0].ToString());
        }
    }
}